//用于判断是否有登录
import { useLocation, useNavigate } from "react-router-dom"
import { useEffect } from "react"
import { isLoginORRegister,isNeedLogin } from "../route"
import { LOGIN_PATHNAME, QUESTION_PATHNAME } from "../concats"
import useGetuserInfo from "./useGetuserInfo"

const useNavigatePage = (waittingdata:boolean) =>{
    const nav = useNavigate()
    const {username} = useGetuserInfo()
    const {pathname} = useLocation()
    useEffect(()=>{
        if(waittingdata){
            return
        }
        if(username){
            //表示已经登录
            if(isLoginORRegister(pathname)){
                nav(QUESTION_PATHNAME)
            }
            return
        }
        if(!isNeedLogin(pathname)){
            return
        }else{
            nav(LOGIN_PATHNAME)
        }
    },[waittingdata])
}

export default useNavigatePage